package com.bdqn.controller;

import com.bdqn.component.BlogConstants;
import com.bdqn.dao.ArticleMapper;
import com.bdqn.dao.ImagesMapper;
import com.bdqn.entity.ArticleWithBLOBs;
import com.bdqn.entity.Images;
import com.bdqn.entity.UserWithBLOBs;
import com.bdqn.service.AlbumService;
import com.bdqn.util.Page;
import com.bdqn.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author SIMBA1949
 * @Date 2020/3/5 11:18
 */
@Controller
@RequestMapping("/album")
public class AlbumController {
    @Autowired
    private AlbumService albumService;


    @PostMapping("/{blog}-album")
    @ResponseBody
    public Result editAlbum(@PathVariable("blog") String blog, Images img,
                            @RequestParam(name = "upload", required = false) CommonsMultipartFile file[],
                            HttpServletRequest request) {
        return albumService.addUpdateAlbum(blog, img, file, request);
    }

    @PostMapping("/{draft}-loadAlbum")
    @ResponseBody
    public Page<Images> loadAlbum(@PathVariable("draft") Boolean draft, String timeRangae, String title,
                                  @RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
                                  @RequestParam(name = "pageSize", defaultValue = "4") Integer pageSize,HttpSession session) {
        UserWithBLOBs loginUser = (UserWithBLOBs) session.getAttribute(BlogConstants.LOGIN_SESSION_KEY);
        return albumService.getPageAlbum(draft, timeRangae, title, pageIndex, pageSize,loginUser);
    }

    @GetMapping("/{flag}-Album")
    public String lookAlbum(@PathVariable("flag") String flag, String uuid, Model model) {
        model.addAttribute("flag", flag);
        model.addAttribute("album", albumService.getAlbumById(uuid));

        return "client/user-editAlbum";
    }


    @PostMapping("/deleteImg")
    @ResponseBody
    public Result deleteImg(String uuidImg, HttpServletRequest request) {

        return albumService.deleteImg(uuidImg, request);
    }


    @PostMapping("/updateCover")
    @ResponseBody
    public Result updateCover(String uuid, @RequestParam(name = "upload", required = false) CommonsMultipartFile file, HttpServletRequest request) {
        return albumService.updateCover(uuid, file, request);
    }


    @PostMapping("/edit/{flag}/{uuid}")
    @ResponseBody
    public Result updateStatus(@PathVariable("flag") String flag, @PathVariable("uuid") String uuid, HttpServletRequest request) {
        return albumService.updateStatus(flag, uuid, request);
    }


}
